﻿$import( "SmitearWebGL.Events.EventPool" );

SmitearWebGL.Events.EventDispatcher = Class.extend(
{
	/******** constructor ********/
	
	init : function()
	{
		/******** private property ********/
		
		this._callBackClasses = [];
		this._eventPool = new SmitearWebGL.Events.EventPool();
		
		this._registClass( "SmitearWebGL.Events.EventDispatcher" );
	}
	,
	
	/******** public method ********/
	
	addEventListener : function( type, callBackObject, listener )
	{
		this._callBackClasses.push( type );
		this._eventPool._bind( type, callBackObject, listener );
	}
	,
	removeEventListener : function( type, listener )
	{
		if ( this._eventPool._unbind( type, listener ) )
		{
			var i = 0, found = false;
			for ( ; i < this._callBackClasses.length; ++ i )
			{
				if ( type == this._callBackClasses[i] )
				{
					this._callBackClasses.splice( i, 1 );
					found = true;
					break;
				}
			}
		}
	}
	,
	dispatchEvent : function( event )
	{
		event.sender = this;
		this._eventPool._invoke( event );
	}
	,
	hasEventListener : function( type )
	{
		var i = 0;
		for ( ; i < this._callBackClasses.length; ++ i )
		{
			if ( type == this._callBackClasses[i] )
			{
				return true;
			}
		}

		return false;
	}
	,
	willTrigger : function( type )
	{
	}
}
);
